package com.shuaitao.newsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuaitao.newsystem.bean.Comments;
import com.shuaitao.newsystem.bean.News;
import com.shuaitao.newsystem.bean.Topic;
import com.shuaitao.newsystem.service.CommentsService;
import com.shuaitao.newsystem.service.NewsService;
import com.shuaitao.newsystem.mapper.NewsMapper;
import com.shuaitao.newsystem.service.TopicService;
import com.shuaitao.newsystem.utils.R;
import com.shuaitao.newsystem.vo.GroupNews;
import com.shuaitao.newsystem.vo.NewsVo;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author 24820
* @description 针对表【news】的数据库操作Service实现
* @createDate 2023-06-21 17:04:45
*/
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News>
    implements NewsService{

    @Autowired
    private TopicService topicService;
    @Autowired
    private CommentsService commentsService;
    @Override
    public List<GroupNews> getGroupNews() {
        List<GroupNews> groupNews = new ArrayList<>();
        List<Integer> ids = Arrays.asList(1, 2, 5);
        List<Topic> list1 = topicService.list(new QueryWrapper<Topic>().in("tid",ids));
        for (Topic topic : list1){
            List<News> list =
                    this.list(new QueryWrapper<News>().eq("ntid",topic.getTid()).orderByDesc("ncreateDate").last("limit 5"));
            GroupNews groupNews1 = new GroupNews();
            groupNews1.setNtid(topic.getTid());
            groupNews1.setTname(topic.getTname());
            groupNews1.setNews(list);
            groupNews.add(groupNews1);
        }

        return groupNews;
    }

    @Override
    public List<News> getByTid(Integer tid) {
        List<News> list = this.list(new QueryWrapper<News>().eq("ntid", tid));
        return list;
    }

    @Override
    public IPage getNewPage(Integer pageNum, Integer tid, String keywords) {
        IPage<News> newsPage = new Page<>(pageNum, 25);
        QueryWrapper<News> newsQueryWrapper = new QueryWrapper<>();
        if(tid != 0) {
            newsQueryWrapper.eq("ntid",tid).orderByDesc("ncreateDate");
        }else {
            newsQueryWrapper.orderByDesc("ncreateDate");
        }
        if (keywords.length() != 0 && keywords!= null) {
            newsQueryWrapper.and(item -> {
                item.like("ntitle",keywords).or().like("nauthor",keywords);
            });
        }
        IPage<News> page = this.page(newsPage,newsQueryWrapper);
        List<News> records = page.getRecords();
        List<NewsVo> list = records.stream().map(item -> {
            NewsVo newsVo = new NewsVo();
            BeanUtils.copyProperties(item, newsVo);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-DD HH:dd:mm");
            String format = null;
            if (item.getNcreatedate() == null) {
                newsVo.setNcreatedate("暂无");
            }else {
                format = simpleDateFormat.format(item.getNcreatedate());
                newsVo.setNcreatedate(format);
            }

            return newsVo;
        }).collect(Collectors.toList());
        Page<NewsVo> newsVoPage = new Page<>();
        BeanUtils.copyProperties(page,newsVoPage);
        Page<NewsVo> newsVoPage1 = newsVoPage.setRecords(list);
        return newsVoPage1;

    }

    @Override
    public R delNews(Integer nid) {
        boolean b1 = commentsService.remove(new QueryWrapper<Comments>().eq("cnid", nid));
        boolean b = this.removeById(nid);

        if (b) {
            return R.ok();
        }
        return R.error(101,"删除失败!!!");
    }
}




